package org.dtrd.modules.achievement.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.achievement.entity.bean.SigninTemplateContent;
import org.dtrd.modules.achievement.entity.bean.SigninTemplateListInfo;
import org.dtrd.modules.achievement.entity.request.SigninTemplateListRequest;
import org.dtrd.modules.achievement.entity.request.SigninTemplatePageRequest;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninPlanService;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninTemplateService;
import org.dtrd.modules.base.service.BaseCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 打卡计划模板 前端控制器
 * </p>
 * @author QR
 * @since 2022-01-04
 */
@Api(tags = "打卡计划模版")
@RestController
@RequestMapping("/signin/template")
public class SigninTemplateController extends BaseController {

    @Autowired
    private IDtrdEntRdSigninTemplateService signinTemplateService;
    @Autowired
    private BaseCommonService baseCommonService;
    @Autowired
    private IDtrdEntRdSigninPlanService signinPlanService;

    @ApiOperation("查询打卡模版详情")
    @GetMapping("get")
    public Result<?> getSigninTemplateDetail(@RequestParam(required = false) Integer id){
        // 默认初始模版
        if (id == null) {
            id = 0;
        }
        SigninTemplateContent template = signinTemplateService.getSigninTemplate(id);
        String logContent = String.format("查询打卡模版详情, 查询条件: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return template == null ? error(STATE_NO_RECORD) : Result.OK(template);
    }

    @ApiOperation(value = "查询打卡计划模板列表", response = SigninTemplateListInfo.class)
    @PostMapping("list")
    public Result<?> getSigninTemplateList(@RequestBody SigninTemplateListRequest request){
        List<SigninTemplateListInfo> list = signinTemplateService.getSigninTemplateList(request);
        String logContent = String.format("查询打卡计划模板列表, 查询条件: %s", request);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation(value = "分页查询打卡计划模板列表", response = SigninTemplateListInfo.class)
    @PostMapping("page")
    public Result<?> getSigninTemplateList(@RequestBody SigninTemplatePageRequest request){
        handlePageRequest(request);
        IPage<SigninTemplateListInfo> page = signinTemplateService.pageSigninTemplate(request);
        String logContent = String.format("分页查询打卡计划模板列表, 查询条件: %s", request);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改打卡计划模板")
    @PostMapping("edit")
    public Result<?> saveOrUpdateSigninTemplate(@RequestBody SigninTemplateContent dto){
        Integer dataId = dto.getDataId();
        String signInName = dto.getSignInName();
        String orgCode = dto.getOrgCode();
        if (signinTemplateService.hasSameTemplateName(dataId, signInName, orgCode)) {
            return Result.error("不能有同名的打卡计划模版");
        }
        if (StringUtils.isBlank(orgCode)){
            return Result.error("orgCode不能为空");
        }
        boolean result = signinTemplateService.saveOrUpdateSigninTemplate(dto);
        if (result && dataId != null) {
            result = signinPlanService.updatePatientSignInPlanContentByTemplateId(dto.getDataId());
        }

        String logContent = String.format("新增或修改打卡计划模板, 编辑内容: %s", dto);
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除打卡计划模板")
    @DeleteMapping("remove")
    public Result<?> removeSigninTemplate(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        if (signinTemplateService.hasRelatePatient(id)) {
            return Result.error("该模版已经关联患者不能删除");
        }
        boolean result = signinTemplateService.removeSigninTemplate(id);
        String logContent = String.format("删除打卡计划模板, 删除id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
